package com.cyj.dream.admin.persistence.manager.systems;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyj.dream.admin.contacts.enums.IsDeleteEnum;
import com.cyj.dream.core.constant.users.UserContext;
import com.cyj.dream.core.constant.users.UserInfoDTO;
import com.cyj.dream.admin.model.request.SysContactsQueryRequest;
import com.cyj.dream.admin.model.system.SysContacts;
import com.cyj.dream.admin.persistence.service.ITbSysContactsService;
import com.cyj.dream.admin.util.IsEmptyUtils;
import com.cyj.dream.core.constant.enums.ResultCode;
import com.cyj.dream.core.exception.BizException;
import com.cyj.dream.core.util.date.AgeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: 通讯录管理器类
 * @BelongsProject: dream-DreamChardonnay
 * @BelongsPackage: com.dream.DreamChardonnay.persistence.manager.systems
 * @Author: ChenYongJia
 * @CreateTime: 2021-05-19 15:30
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysContactsManager {

    @Autowired
    private ITbSysContactsService iTbSysContactsService;

    /**
     * 查询通讯录信息列表
     *
     * @param page         数据
     * @param queryRequest 请求入参参数
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.dream.DreamChardonnay.model.system.SysContacts>
     * @date 2021/5/20 9:50
     * @author ChenYongJia
     * @version 1.0
     */
    public Page<SysContacts> list(Page<SysContacts> page, SysContactsQueryRequest queryRequest) {
        log.info("进入 SysContactsManager 查询通讯录信息列表，参数 page：{}，参数 param：{}", JSONObject.toJSONString(page), queryRequest);
        LambdaQueryWrapper<SysContacts> wrapper = this.checkMethod(queryRequest);
        wrapper.orderByDesc(true, SysContacts::getCreateTime);

        return iTbSysContactsService.page(page, wrapper);
    }


    /**
     * 不分页查询通讯录信息
     *
     * @param queryRequest 参数以及入参
     * @return java.util.List<com.dream.DreamChardonnay.model.system.SysContacts>
     * @date 2021/7/1 16:11
     * @author ChenYongJia
     * @version 1.0
     */
    public List<SysContacts> allSysContactsByParam(SysContactsQueryRequest queryRequest) {
        log.info("进入 SysContactsManager 不分页查询通讯录信息管理器，参数 queryRequest 为：{}", JSONObject.toJSONString(queryRequest));
        LambdaQueryWrapper<SysContacts> wrapper = this.checkMethod(queryRequest);
        return iTbSysContactsService.list(wrapper);
    }

    /**
     * 根据通讯录id查询通讯录信息
     *
     * @param sysContacts 通讯录对象
     * @return com.dream.DreamChardonnay.model.system.SysContacts
     * @date 2021/5/19 15:35
     * @author ChenYongJia
     * @version 1.0
     */
    public SysContacts selectBySysContactsId(SysContacts sysContacts) {
        log.info("进入 SysContactsManager 根据通讯录id查询通讯录信息管理器，参数为：{}", JSONObject.toJSONString(sysContacts));
        LambdaQueryWrapper<SysContacts> wrapper = new LambdaQueryWrapper<>();

        if (!IsEmptyUtils.isEmpty(sysContacts.getDreamId())) {
            wrapper.eq(SysContacts::getDreamId, sysContacts.getDreamId());
        }
        if (!IsEmptyUtils.isEmpty(sysContacts.getSysContactsName())) {
            wrapper.like(SysContacts::getSysContactsName, sysContacts.getSysContactsName());
        }

        wrapper.eq(SysContacts::getDelFlag, IsDeleteEnum.DIS_DELETE.getCode());

        return iTbSysContactsService.getOne(wrapper);
    }

    /**
     * 添加通讯录信息
     *
     * @param sysContacts 通讯录对象
     * @return boolean
     * @date 2021/5/19 15:47
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean addSysContacts(SysContacts sysContacts) {
        log.info("进入 SysContactsManager 添加通讯录信息管理器，参数为：{}", JSONObject.toJSONString(sysContacts));
        try {
            UserInfoDTO userInfoDTO = UserContext.get();
            // 后台自动计算年龄
            if (!IsEmptyUtils.isEmpty(sysContacts.getSysContactsBirthday())) {
                sysContacts.setSysContactsAge(AgeUtils.getCurrentAge(sysContacts.getSysContactsBirthday()));
            }

            sysContacts.setCreateTime(LocalDateTime.now());
            sysContacts.setCreateUserId(userInfoDTO.getSysUserId());
            return iTbSysContactsService.save(sysContacts);
        } catch (Exception e) {
            log.error("调用修改用户相关信息管理器方法失败");
            throw new BizException(ResultCode.ERROR);
        }
    }

    /**
     * 修改通讯录信息
     *
     * @param sysContacts 通讯录对象
     * @return boolean
     * @date 2021-5-19 15:48:03
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean updateSysContacts(SysContacts sysContacts) {
        log.info("进入 SysContactsManager 修改通讯录信息管理器，参数为：{}", JSONObject.toJSONString(sysContacts));
        try {
            UserInfoDTO userInfoDTO = UserContext.get();
            // 后台自动计算年龄
            if (!IsEmptyUtils.isEmpty(sysContacts.getSysContactsBirthday())) {
                sysContacts.setSysContactsAge(AgeUtils.getCurrentAge(sysContacts.getSysContactsBirthday()));
            }

            sysContacts.setUpdateTime(LocalDateTime.now());
            return iTbSysContactsService.updateById(sysContacts);
        } catch (Exception e) {
            log.error("调用修改用户相关信息管理器方法失败");
            throw new BizException(ResultCode.ERROR);
        }
    }

    /**
     * 删除通讯录信息
     *
     * @param sysContacts 通讯录对象集合
     * @return boolean
     * @date 2021/5/19 15:50
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean deleteSysContacts(List<SysContacts> sysContacts) {
        log.info("进入 SysContactsManager 删除通讯录信息管理器，参数为：{}", JSONObject.toJSONString(sysContacts));

        try {
            UserInfoDTO userInfoDTO = UserContext.get();
            sysContacts.stream().forEach(item -> {
                item.setUpdateUserId(userInfoDTO.getSysUserId());
                item.setUpdateTime(LocalDateTime.now());
                item.setDelFlag(IsDeleteEnum.IS_DELETE.getCode());
            });
            return iTbSysContactsService.updateBatchById(sysContacts);
        } catch (Exception e) {
            log.error("调用删除通讯录信息管理器方法失败，参数为：{}", e);
            throw new BizException(ResultCode.ERROR);
        }
    }

    /**
     * 检查统一需要验证的参数
     *
     * @param queryRequest 请求参数
     * @return com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<com.dream.DreamChardonnay.model.system.SysContacts>
     * @date 2021/7/1 16:15
     * @author ChenYongJia
     * @version 1.0
     */
    private LambdaQueryWrapper<SysContacts> checkMethod(SysContactsQueryRequest queryRequest) {
        log.info("进入 SysContactsManager 查询通讯录信息列表，参数 param：{}", JSONObject.toJSONString(queryRequest));
        LambdaQueryWrapper<SysContacts> wrapper = new LambdaQueryWrapper<SysContacts>();
        if (!IsEmptyUtils.isEmpty(queryRequest)) {
            // 通讯录用户名称
            if (!IsEmptyUtils.isEmpty(queryRequest.getSysContactsName())) {
                wrapper.like(SysContacts::getSysContactsName, queryRequest.getSysContactsName());
            }
            // 通讯录公司名称
            if (!IsEmptyUtils.isEmpty(queryRequest.getSysContactsCompany())) {
                wrapper.like(SysContacts::getSysContactsCompany, queryRequest.getSysContactsCompany());
            }
            // 通讯录用户业务类型
            if (!IsEmptyUtils.isEmpty(queryRequest.getSysContactsBusinessTypes())) {
                wrapper.eq(SysContacts::getSysContactsBusinessTypes, queryRequest.getSysContactsBusinessTypes());
            }
        }
        wrapper.eq(SysContacts::getDelFlag, IsDeleteEnum.DIS_DELETE.getCode());

        return wrapper;
    }


}
